﻿using System.IO;
using System.Reflection;
using EnvDTE;
using EnvDTE80;
using Microsoft.Practices.RecipeFramework;

namespace ProjectBase.Guidance.Actions
{
    public class CopySolutionFolders : ConfigurableAction
    {
        private void CopyDirectory(DTE2 dte, string source, string target, Project ra)
        {
            if (!Directory.Exists(target))
            {
                Directory.CreateDirectory(target);
            }
            string[] parts = target.Split(Path.DirectorySeparatorChar);
            string dest;
            foreach (string src in Directory.GetFiles(source))
            {
                dest = Path.Combine(target, Path.GetFileName(src));
                File.Copy(src, dest);
                if (ra != null)
                {
                    ra.ProjectItems.AddFromFile(dest);
                }
            }
            SolutionFolder sf = null;
            Project tempProject = null;
            foreach (string d in Directory.GetDirectories(source))
            {
                parts = d.Split(Path.DirectorySeparatorChar);
                sf = ra.Object as SolutionFolder;
                if (sf != null)
                {
                    tempProject = sf.AddSolutionFolder(parts[parts.Length - 1]);
                }
                CopyDirectory(dte, d, Path.Combine(target, parts[parts.Length - 1]), tempProject);
            }
        }

        public override void Execute()
        {
            CreateAssembly();
            CreateSql();
        }

        protected virtual void CreateAssembly()
        {
            string source = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
                                         @"Templates\Solutions\.nuget");
            if (Directory.Exists(source))
            {
                var dte = GetService<DTE>(true) as DTE2;
                string target =
                    Path.Combine(Path.GetDirectoryName(dte.Solution.Properties.Item("Path").Value.ToString()),
                                 ".nuget");
                var sol = dte.Solution as Solution2;
                Project ra = sol.AddSolutionFolder(".nuget");
                CopyDirectory(dte, source, target, ra);
                dte.Documents.CloseAll(vsSaveChanges.vsSaveChangesNo);
            }
        }

        protected virtual void CreateSql()
        {
            string source = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
                                         @"Templates\Solutions\SQLServer");
            if (Directory.Exists(source))
            {
                var dte = GetService<DTE>(true) as DTE2;
                string target =
                    Path.Combine(Path.GetDirectoryName(dte.Solution.Properties.Item("Path").Value.ToString()),
                                 "SQLServer");
                var sol = dte.Solution as Solution2;
                Project ra = sol.AddSolutionFolder("SQLServer");
                CopyDirectory(dte, source, target, ra);
                dte.Documents.CloseAll(vsSaveChanges.vsSaveChangesNo);
            }
        }

        public override void Undo()
        {
        }
    }
}